<?php

namespace Crc\Grid\Filter;

use Crc\Grid\Filter;

class Entity extends Filter
{
    protected $_operatorsText = array('Is');

    protected $_entity;

    protected $_valueField;

    protected $_labelField;

    public function setEntity($_entity)
    {
        $this->_entity = $_entity;
        return $this;
    }

    public function setValueField($_valueField)
    {
        $this->_valueField = $_valueField;
        return $this;
    }

    public function setLabelField($_labelField)
    {
        $this->_labelField = $_labelField;
        return $this;
    }

    public function getData()
    {
        $doctrine = \Zend_Registry::get('doctrine');
        $entityManager = $doctrine->getEntityManager();
        $repository = $entityManager->getRepository('\Crc\Entity\\' . $this->_entity);
        return $repository->findAll();
    }

    public function getDataValue($data)
    {
        $function = 'get' . ucfirst($this->_valueField);
        return $data->$function();
    }

    public function getDataLabel($data)
    {
        if ($this->_labelField === NULL) {
            $result = (string)$data;
            return $result;
        }

        $function = 'get' . ucfirst($this->_labelField);
        return $data->$function();
    }
}